using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Collider))]
public class DestroyableObstacle : MonoBehaviour
{
	[Header("Система частиц при слабом столкновении")]
	public string smallParticleName;

	public float smallParticleLifeTime = 1f;

	[Header("Система частиц при сильном столкновении (сносе)")]
	public string largeParticleName;

	public float largeParticleLifeTime = 2f;

	[Header("Скоростные промежутки от слабого столкновения до сноса")]
	public float hitSpeed = 50f;

	public float destroySpeed = 80f;

	private MeshRenderer meshRenderer;

	private List<Collider> colliders = new List<Collider>();

	private void OnEnable()
	{
		if (meshRenderer == null)
		{
			meshRenderer = GetComponentInChildren<MeshRenderer>();
		}
		colliders.AddRange(GetComponentsInChildren<Collider>());
		TurnOnOff(true);
	}

	private void TurnOnOff(bool isTurningOn)
	{
		if (meshRenderer != null)
		{
			meshRenderer.enabled = isTurningOn;
		}
		colliders.ForEach(delegate(Collider x)
		{
			x.enabled = isTurningOn;
		});
	}

	private void OnTriggerEnter(Collider other)
	{
		EntityBehavior componentInParent = other.GetComponentInParent<EntityBehavior>();
		if (!(componentInParent == null))
		{
			float currentSpeed = componentInParent.GetCurrentSpeed();
			if (currentSpeed >= hitSpeed && currentSpeed < destroySpeed)
			{
				Object.Destroy(CreateParticle(smallParticleName), smallParticleLifeTime);
			}
			else if (currentSpeed >= destroySpeed)
			{
				Object.Destroy(CreateParticle(largeParticleName), largeParticleLifeTime);
				TurnOnOff(false);
			}
		}
	}

	private GameObject CreateParticle(string poolName)
	{
		GameObject gameObject = PoolManager.instance.TakeFromPool(poolName, base.transform);
		gameObject.transform.localRotation = Quaternion.identity;
		gameObject.transform.localPosition = new Vector3(0f, 2f, 0f);
		gameObject.SetActive(true);
		return gameObject;
	}
}
